#include "WMarkConfig.h"
#include "log/log.h"
namespace android
{
    WMarkConfig *WMarkConfig::mSingleton = NULL;

    // 默认配置
    int WMarkConfig::mFontSzie = 0;              // 字体大小
    int WMarkConfig::mWatermarkImageHeight = 0;  // 生成水印图片的 高， 尽量和字体大小保持一致即可
    int WMarkConfig::mWatermarkImageWidth = 0; // 生成水印图片的 宽,  尽量和分辨率的宽保持一致即可
    bool WMarkConfig::mEnableWatermark = false; // 指定是否启用水印功能
    string WMarkConfig::mFontLibPath = ""; // 指定字体库的路径
    string WMarkConfig::mConfigJsonPath = "/vendor/etc/WaterMarkConfig.json";

    int WMarkConfig::mMultiCameraDisplay = 0;
    int WMarkConfig::mStrBgColorEnable = 0;
    int WMarkConfig::mStrBgColorR = 0;
    int WMarkConfig::mStrBgColorG = 0;
    int WMarkConfig::mStrBgColorB = 0;
    float WMarkConfig::mStrBgColorA = 0.0;

    int WMarkConfig::mPenX = 0;
    int WMarkConfig::mPenY = 0;

    WMarkConfig *WMarkConfig::getInstance()
    {
        if (mSingleton == NULL)
        {
            mSingleton = new WMarkConfig();
        }
        return mSingleton;
    }

    WMarkConfig::WMarkConfig()
    {
        Json::Value root;
        Json::Reader reader;
        std::ifstream ifs(mConfigJsonPath); //open file example.json

        if (!reader.parse(ifs, root))
        {
            // fail to parse
            ALOGE("fail to parse: %s\n", mConfigJsonPath.c_str());
            mEnableWatermark = false; // 关闭 水印功能
        }
        else
        {
            mFontSzie = root["font-szie"].asInt();
            mWatermarkImageHeight= root["watermark-image-height"].asInt();
            mWatermarkImageWidth= root["watermark-image-width"].asInt();
            mEnableWatermark = root["enable-watermark"].asBool();
            mFontLibPath = root["font-lib-path"].asString();
            mMultiCameraDisplay = root["display"]["multi_channel"].asInt();
            mStrBgColorEnable = root["str_background_color"]["enable"].asInt();
            mPenX = root["penX"].asInt();
            mPenY = root["penY"].asInt();
        }
        if (mStrBgColorEnable) {
            mStrBgColorR = root["str_background_color"]["r"].asInt();
            mStrBgColorG = root["str_background_color"]["g"].asInt();
            mStrBgColorB = root["str_background_color"]["b"].asInt();
            mStrBgColorA = root["str_background_color"]["a"].asFloat();
        }
        printConfig();
    }

    WMarkConfig::~WMarkConfig()
    {
    }

    void WMarkConfig::printConfig() {
        ALOGD("++++++++++++++++++++++++++++++++");
        ALOGD("- enable : %s", mEnableWatermark ? "on": "off");
        ALOGD("- font-size : %d", mFontSzie);
        ALOGD("- watermark-image : %dx%d", mWatermarkImageHeight, mWatermarkImageWidth);
        ALOGD("- font-path : %s", mFontLibPath.c_str());
        ALOGD("- multi-channel_display : %d", mMultiCameraDisplay);
        ALOGD("- strBgColor-on : %d rgba( %d, %d, %d, %f)", mStrBgColorEnable, mStrBgColorR, mStrBgColorG, mStrBgColorB, mStrBgColorA);
        ALOGD(" ++++++++++++++++++++++++++++++++");
    }

    int WMarkConfig::getStrBackgroundColor(StrBgColor *strBgColor) {
        if (!strBgColor) {
            ALOGE("%s strBgColor == NULL", __func__);
            return -1;
        }
        strBgColor->enable = mStrBgColorEnable;
        strBgColor->r = mStrBgColorR;
        strBgColor->g = mStrBgColorG;
        strBgColor->b = mStrBgColorB;
        strBgColor->a = mStrBgColorA;
        return 0;
    }

    int WMarkConfig::getPenXY(int *x, int *y) {

        if (!x || !y) {
            ALOGE("%s x || y == NULL", __func__);
            return -1;
        }
        *x = mPenX;
        *y = mPenY;
        return 0;
    }
}
